package com.qm.util;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;

import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.web.multipart.MultipartFile;

import com.qm.model.ImportExcelReprotModel;
import com.qm.model.Topic;

public class TopicExcelImport {
public static ArrayList<ImportExcelReprotModel<Topic>> parseTopic(MultipartFile excel) throws IOException {
		
		//InputStream file=new FileInputStream("123.xls");
		XSSFWorkbook hssfWorkbook=new XSSFWorkbook(excel.getInputStream());//新建一个excel对象
		XSSFSheet sheet=hssfWorkbook.getSheetAt(0);//去对象的第一页，可以说是第一张表
		XSSFRow headRow=sheet.getRow(0);//取表的第一行，即表头信息
		HashMap<Integer, String> indexMap=new  HashMap<Integer,String>();//创建一个hash表，来存储表头对应的信息
		int colCount=headRow.getPhysicalNumberOfCells();//获取表头的长度
		for(int i=0;i<colCount;i++){//把表头对应的列数和该列对应的字段名存入hash
			XSSFCell cell=headRow.getCell(i);
			String cellValue=cell.getStringCellValue();
			if(!cellValue.isEmpty()&&!indexMap.containsValue(cellValue)){
				indexMap.put(i, cellValue);
			}
		}
		int rowCount=sheet.getPhysicalNumberOfRows();//查询这张表有多少行
		ArrayList<ImportExcelReprotModel<Topic>>  topics=new ArrayList<>();
		for(int i=1;i<rowCount;i++){//从第二行开始，把数据存入list中
			XSSFRow row=sheet.getRow(i);//读表的第i行数据
			Topic topic=new Topic();
		    ImportExcelReprotModel<Topic> report=new ImportExcelReprotModel<>();
		    report.setData(topic);
		    topics.add(report);
		    for(int j=0;j<colCount;j++){//表第i行数据的第j列数据
		    	XSSFCell cell=row.getCell(j);
		    	String methodName=indexMap.get(j);
		    	if(methodName==null||methodName.trim().isEmpty()){
					continue;
				}
		    	if(methodName.equals("题目类型")){
		    		topic.setTtopicType(getValue(cell));
		    	}
		    	else if(methodName.equals("题干")){
		    		topic.setTtopic(getValue(cell));
		    	}
		    	else if(methodName.equals("正确答案")){
		    		topic.setTcorrect(getValue(cell));
		    	}
		    	else if(methodName.equals("错误答案1")){
		    		topic.setTmistake1(getValue(cell));
		    	}
		    	else if(methodName.equals("错误答案2")&&methodName!=null){
		    		try {
			    		topic.setTmistake2(getValue(cell));
					} catch (Exception e) {
						topic.setTmistake2(null);
					}
		    	}
		    	else if(methodName.equals("错误答案3")&&methodName!=null){
		    		try {
		    			topic.setTmistake3(getValue(cell));
					} catch (Exception e) {
						topic.setTmistake3(null);
					}
		    	}
		    	else if(methodName.equals("答案解析")&&methodName!=null){
		    		try {
			    		topic.setTanswerAnalysis(getValue(cell));
					} catch (Exception e) {
						topic.setTanswerAnalysis(null);
					}
		    	}
		    }
		    if(topic.getTtopicType()==null||topic.getTtopicType().trim().isEmpty()){
		    	report.setErrorMsg("题目类型不能为空");
		    	report.setErrorLine(i+1);
		    	continue;
		    }
		    if(topic.getTtopic()==null||topic.getTtopic().trim().isEmpty()){
		    	report.setErrorMsg("题干不能为空");
		    	report.setErrorLine(i+1);
		    	continue;
		    }
		    if(topic.getTcorrect()==null||topic.getTcorrect().trim().isEmpty()){
		    	report.setErrorMsg("正确答案不能为空");
		    	report.setErrorLine(i+1);
		    	continue;
		    }
		    if(topic.getTmistake1()==null||topic.getTtopic().trim().isEmpty()){
		    	report.setErrorMsg("错误答案不能为空");
		    	report.setErrorLine(i+1);
		    	continue;
		    }
		}
	
		return topics;
	}
	private static String getValue(XSSFCell xssfCell){//判断返回什么类型
		if(xssfCell==null){
			return "";
		}
		if(xssfCell.getCellType()==XSSFCell.CELL_TYPE_BOOLEAN){
			//返回布尔型的值
			return String.valueOf(xssfCell.getBooleanCellValue());
			
		}
		else if(xssfCell.getCellType()==XSSFCell.CELL_TYPE_NUMERIC){
			return String.valueOf((int)xssfCell.getNumericCellValue());
			
		}
		else{
			return String.valueOf(xssfCell.getStringCellValue());
		}
	}


}
